function e(t) {
    n.showLoading("正在获取数据"), wx.request({
        url: i.globalData.host + "/lotto/v1/client_info",
        data: {
            access_token: wx.getStorageSync("accessToken")
        },
        method: "GET",
        success: function(a) {
            if (n.hideLoading(), -500 != a.data.sta) if (0 == a.data.sta) {
                var o = a.data.data;
                t.setData({
                    name: o.name,
                    phone: o.phone,
                    email: o.email,
                    address: o.address,
                    post: o.post_code,
                    defaultName: o.name,
                    defaultPhone: o.phone,
                    oldPhone: o.phone,
                    defaultEmail: o.email,
                    defaultAddress: o.address,
                    defaultPost: o.post_code
                });
            } else n.showFailedToast("获取数据失败", a.data.msg); else n.login(function() {
                e(t);
            });
        },
        fail: function(e) {
            n.hideLoading(), n.showFailedToast("获取数据失败");
        }
    });
}

function t(e) {
    n.showLoading("正在保存数据");
    var a = e.data.phone, o = e.data.code;
    o = o ? parseInt(o) : "", o = 1 == e.data.isDev ? 9999999 : o;
    var s = {
        access_token: wx.getStorageSync("accessToken"),
        name: e.data.name,
        code: o,
        email: e.data.email,
        address: e.data.address,
        post_code: e.data.post
    };
    a != e.data.oldPhone && (s.phone = a), wx.request({
        url: i.globalData.host + "/lotto/v1/client_info",
        data: s,
        method: "POST",
        success: function(o) {
            if (n.hideLoading(), -500 != o.data.sta) if (0 == o.data.sta) {
                var s = n.getStorage("userInfo"), i = s.nickName, d = s.avatarUrl;
                n.setStorage("userInfo", {
                    nickName: i,
                    avatarUrl: d,
                    phone: a
                }), wx.navigateBack({
                    delta: 1
                });
            } else n.showFailedToast("数据保存失败", o.data.msg); else n.login(function() {
                t(e);
            });
        },
        fail: function(e) {
            n.hideLoading(), n.showFailedToast("数据保存失败");
        }
    });
}

function a(e) {
    n.showLoading("正在获取验证码..."), wx.request({
        url: i.globalData.host + "/lotto/v1/code",
        data: {
            phone: e.data.phone,
            access_token: wx.getStorageSync("accessToken")
        },
        method: "POST",
        success: function(t) {
            n.hideLoading(), -500 != t.data.sta ? 0 == t.data.sta ? o(e) : n.showFailedToast("获取验证码失败，请重试", t.data.msg) : n.login(function() {
                a(e);
            });
        },
        fail: function(e) {
            n.hideLoading(), n.showFailedToast("获取验证码失败，请重试");
        }
    });
}

function o(e) {
    var t = e.data.verifyCodeCountdownNum - 1;
    t > 0 ? (e.setData({
        verifyCodeCountdownNum: t,
        verifyCodeText: t + "后重新获取"
    }), setTimeout(function() {
        o(e);
    }, 1e3)) : e.setData({
        verifyCodeCountdownNum: 60,
        verifyCodeText: "获取验证码"
    });
}

function s(e) {
    wx.chooseLocation({
        success: function(t) {
            e.setData({
                address: t.address,
                defaultAddress: t.address
            });
        },
        fail: function(t) {
            var a = t.errMsg;
            a && (a.indexOf("fail auth") > -1 || a.indexOf("fail:auth") > -1) && (wx.openSetting ? wx.showModal({
                title: "提示",
                content: "请授权抽奖小助手访问您的地理位置，该操作仅会方便您获取地址，请放心授权。",
                cancelText: "不想授权",
                confirmText: "去授权",
                confirmColor: "#ff5353",
                success: function(t) {
                    t.confirm ? wx.openSetting({
                        complete: function(t) {
                            s(e);
                        }
                    }) : t.cancel && n.showModelTips("如果您不愿授权，也可以自己填写所在位置");
                }
            }) : n.showModelTips("由于您拒绝授权微友名片访问您的地理位置，因此无法获取当前位置，请自己填写"));
        }
    });
}

var n = require("../../utils/util.js"), i = getApp();

Page({
    data: {
        name: n.getStorage("userInfo").nickName,
        defaultName: n.getStorage("userInfo").nickName,
        phone: "",
        code: "",
        email: "",
        address: "",
        post: "",
        verifyCode: "",
        verifyCodeEmpty: !0,
        verifyCodeCountdownNum: 60,
        verifyCodeText: "获取验证码"
    },
    onLoad: function(t) {
        console.log(t), this.setData({
            isDev: t.is_dev || 0
        }), e(this);
    },
    onShow: function() {},
    inputName: function(e) {
        this.setData({
            name: e.detail.value
        });
    },
    clearName: function(e) {
        this.setData({
            name: "",
            defaultName: ""
        });
    },
    inputPhone: function(e) {
        this.setData({
            phone: e.detail.value
        });
    },
    clearPhone: function(e) {
        this.setData({
            phone: "",
            defaultPhone: ""
        });
    },
    inputCode: function(e) {
        this.setData({
            code: e.detail.value
        });
    },
    clearCode: function(e) {
        this.setData({
            code: "",
            defaultCode: ""
        });
    },
    inputEmail: function(e) {
        this.setData({
            email: e.detail.value
        });
    },
    clearEmail: function(e) {
        this.setData({
            email: "",
            defaultEmail: ""
        });
    },
    inputAddress: function(e) {
        this.setData({
            address: e.detail.value
        });
    },
    clearAddress: function(e) {
        this.setData({
            address: "",
            defaultAddress: ""
        });
    },
    inputPost: function(e) {
        this.setData({
            post: e.detail.value
        });
    },
    clearPost: function(e) {
        this.setData({
            post: "",
            defaultPost: ""
        });
    },
    getVerifyCode: function(e) {
        var t = this.data.phone;
        n.isTextEmpty(t) ? n.showModelTips("请输入手机号") : (this.setData({
            phone: t
        }), a(this));
    },
    chooseLocation: function() {
        s(this);
    },
    userInfo: function(e) {
        n.postFormId(e.detail.formId), n.buttonClicked(this);
        var a = this.data.phone, o = this.data.oldPhone, s = this.data.code;
        "" == a || a == o || "" != s ? t(this) : n.showModelTips("请输入验证码");
    }
});